<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Grid
*/
class DomFgGridStatusField extends DomFgGridField {
	
	protected $statuses;
	
	protected $style;
	
	protected $colorTextOnly;
	
	/**
	* Describing statuses
	*
	* Sample 1:
	* array(0=>array('Bad','red'), 1=>array('Good','green'), 2=>array('Warning', 'yellow'))
	* @param Array $value
	* @return DomFgGridStatusField
	*/
	public function statuses( array $statuses ) {
		$this->statuses = $statuses;
		return $this;
	}
	/**
	 * Colors only text. (Will not make border around it and color background).
	 */
	public function colorTextOnly( $value = true) {
		$this->colorTextOnly = $value;
		return $this;
	}
	
	public function style( $style ) {
		$this->style = $style;
		return $this;
	}
	
	public function getValue( &$row, $i ) {
		
		$value = parent::getValue( $row, $i );
		//if( is_bool($value['value']) )
		//	$value['value'] = (int)$value['value'];

		if( isset($this->statuses[$value['value']]) ) {
			$r = new DomSmarty();
			$r->template_dir = DomFg::getTemplatePath();
			$r->assign('colorTextOnly', $this->colorTextOnly );
			$r->assign('templateUrl', DomFg::getTemplateUrl() );
			$r->assign('style', $this->style );
			$r->assign('message', $this->statuses[$value['value']][0] );
			$r->assign('color', $this->statuses[$value['value']][1] );
			$value['value'] = $r->fetch('GridFields/Status.html');
		} else {
			$value['value'] = 'Unknown status';
		}
		
		return $value;	
	}

}
?>